package com.tidb.example.demo.service;

import com.tidb.example.demo.entity.User;
import com.tidb.example.demo.mapper.UserMapper;
import com.tidb.example.demo.util.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : ${author}
 * @version V1.0
 * @Package ${packageName}
 * @Description: todo
 * @date ${date} ${time}
 */
@Service
public class UserService {
      @Autowired
      private UserMapper userMapper ;

      @Autowired
      private JdbcTemplate jdbcTemplate ;
      @Autowired
      private NamedParameterJdbcTemplate template ;

      public User selectByUname ( String uname ) {
            System.out.println("service name : "+ uname) ;
            User user ;
            user = userMapper.selectByUname( uname) ;
            return user ;
      }

      public List<User> selectAll () {
            List<User> userList = new ArrayList<>() ;
            userList = userMapper.selectAll() ;
            return userList ;
      }

      public int insertOne (User user) {
            return userMapper.insertOne(user) ;
      }

      public int updateById ( int id, User user ) {
            return userMapper.updateById( id , user) ;
      }

      public int deleteById ( int id ) {
            return userMapper.deleteById( id ) ;
      }

      public int batchSave(List<User> users, int number) {
            long start, end ;
            start=System.currentTimeMillis() ;
            for (int i = 0 ; i < number ; i ++){
                  users.add(new User("小明", "男", new Date())) ;
            }
            end=System.currentTimeMillis() ;
            System.out.println("生成数据耗时: "+ (end-start));
//            long s =System.currentTimeMillis() ;
            int result = userMapper.batchSave(users) ;
//            long e =System.currentTimeMillis() ;
//            System.out.println("插入数据耗时：" + (e-s));
            return result ;
      }

      public int batchSave2(List<User> users, int number) throws InterruptedException {
            for (int i=0 ; i < 10000 ; i++ ){
                  users.add(new User("小明", "男", new Date())) ;
            }
//            final int[] result = {0};
            System.out.println(" number= " + number);

            ExecutorService executor = Executors.newFixedThreadPool(20);
//            int step = number/20 ;
            CountDownLatch count = new CountDownLatch(number/500000) ; //一个线程50万
            long start = System.currentTimeMillis() ;
            for (int i=0; i < number; i+=500000){//一个线程10万
                  executor.execute(new ThreadUtil(i,userMapper, users, count));
            }
            count.await();

//            for (int i =0 ; i < number ; i+=10000 ) {
//                 if ((i+5000) > number) {
//                     result=  userMapper.batchSave(users.subList(i, number)) ;
//                 }else{
//                      result= userMapper.batchSave(users.subList(i, i+5000)) ;
//                 }
//            }
            System.out.println("插入耗时： "+ (System.currentTimeMillis()-start)/1000) ;

            return 1;
      }

      public int[] batchInsert(List<User> users,int number) {
            System.out.println("batchInsert#进入");
            SqlParameterSource[] sqlParameterSources =
                    SqlParameterSourceUtils.createBatch(users.toArray()) ;
            List<Object[]> batchArgs=new ArrayList<Object[]>();

            for (int i = 0 ; i < number ; i ++){
//                  users.add(new User("小明", "男", new Date())) ;
                  batchArgs.add(new Object[]{"小明", "男", new Date()});

            }
            System.out.println("数据生成#开始计时");
//            String sql = "insert into user_t(uname, gender, birthday) values(:uname, :gender, :birthday)" ;
            String sql = "insert into user_t(uname, gender, birthday) values(?, ?, ?)" ;
            long start = System.currentTimeMillis() ;
//            for (int i =0 ; i < number ; i+=10000){
//                  new Thread(){
//
//                  }.start();
//            }
            int[] result = jdbcTemplate.batchUpdate(sql,batchArgs);

            long time = System.currentTimeMillis()-start ;
            System.out.println("sql执行时间为：" + time ) ;
            return result ;
      }


      public int batchDelete(List idList, int startId, int endId) {
            for (int i= startId; i < endId; i++){
                  idList.add(i) ;
            }
            return userMapper.batchDelete(idList) ;
      }
}
